<template>
  <section>
      <el-table
                :data="list"
                row-key="id"
                style="width: 100%">
            <el-table-column align="left" label="分类名称">
                <template slot-scope="scope">
                    <span>{{scope.row.name}}</span>
                </template>
            </el-table-column>
            <el-table-column align="center" label="分类图片">
                <template slot-scope="scope">
                    <img v-if="scope.row.picture" :src="scope.row.picture | thumbnail" :data-origin="scope.row.picture" class="img-thumbnail" width="60" />
                </template>
            </el-table-column>
            <el-table-column align="left" label="商品数">
                <template slot-scope="scope">
                    <span>{{scope.row.num}}</span>
                </template>
            </el-table-column>
        </el-table>
  </section>
</template>

<script>
    import {
        product_cate_nums
    } from '../../api/product';
    import {
        agency_cat_tree
    } from '../../api/agency';
    export default {
        components: {
        },
        data() {
            return {
                list: []
            }
        },
        mounted() {
        
        },
        created() {
            this.getCateTree();
        },
        methods: {
            getCateTree() {
                agency_cat_tree().then((res)=>{
                    let data = res.data;

                    this.list = [];
                    if(data && data.length > 0) {
                        product_cate_nums().then((res2)=>{
                            let data2 = res2.data;
                            if(!data2 || data2.length < 1) {
                                data2 = [];
                            }

                            data.forEach((item)=>{
                                item.num = 0;
                                data2.forEach((item2)=>{
                                    if(item2.cateId == item.id) {
                                        item.num = item2.num;
                                        return false;
                                    }
                                });

                                if(item.children && item.children.length > 0) {
                                    item.children.forEach((citem)=>{
                                        citem.num = 0;

                                        data2.forEach((item2)=>{
                                            if(item2.cateId == citem.id) {
                                                citem.num = item2.num;
                                                return false;
                                            }
                                        });
                                    })
                                }
                            });

                            this.list = data;
                            // console.log(data);
                        }).catch((err2)=>{

                        })
                    }
                }).catch((err)=>{

                })
            }
        }
    };
</script>
